import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class FollowUpService {
  headers = {
    baseUrl: environment.VALUE_KEY_URL_CCZX,
    Authorization: JSON.parse((localStorage as any).getItem('ppypToken').toString())
  };

  serviceUrls: any = {
    dictUrl: '/dict/detail', // 字典查询
    uploadFileUrl: '/common/upload/file', // 上传

    adzFollowerListUrl: '/adzFollower/list', // 爱定制跟进记录list
    followerListUrl: '/follower/list', // 订单跟进记录list
    recycleFollowerListUrl: '/recycleFollower/list', // 二手商城跟进订单list
 
    adzFollowerSaveUrl: '/adzFollower/save', // 爱定制订单跟进提交
    followerSaveUrl: '/follower/save', // 订单跟进提交
    recycleFollowerSaveUrl: '/recycleFollower/save', // 二手商城订单跟进提交
  };

  constructor(public httpClient: HttpClient) { }

  /**
   * 订单记录提交
   * @param params 
   * @param type [ohterParams: { type }] 0: 订单 1: 二手商城 2：爱定制
   * @returns 
   */
  saveFollowerItem(params: any, type: string | number, sourceType: string | number = 66) {
    let url: string = '';
    switch (type) {
      case 0:
        url = this.serviceUrls.followerSaveUrl;
        break;
      case 1:
        url = this.serviceUrls.recycleFollowerSaveUrl;
        break;
      case 2:
        url = this.serviceUrls.adzFollowerSaveUrl;
        break;
    }
    // sourceType 66: 配件商城(默认) 99: 仓储中心
    return this.httpClient.post(url, params, {headers: sourceType === 99 ? this.headers : {}}).toPromise();
  }

  /**
   * 订单记录查询
   * @param params 
   * @param type [ohterParams: { type }] 0: 订单 1: 二手商城 2：爱定制
   * @returns 
   */
  queryFollowerListData(params: any, type: string | number, sourceType: string | number = 66) {
    let url: string = '';
    switch (type) {
      case 0:
        url = this.serviceUrls.followerListUrl;
        break;
      case 1:
        url = this.serviceUrls.recycleFollowerListUrl;
        break;
      case 2:
        url = this.serviceUrls.adzFollowerListUrl;
        break;
    }
    // sourceType 66: 配件商城(默认) 99: 仓储中心
    return this.httpClient.post(url, params, {headers: sourceType === 99 ? this.headers : {}}).toPromise();
  }

  /**
   * 文件上传
   * @param fileToUpload 
   * @returns 
   */
  uploadFileData(fileToUpload: File, fileName?: any) {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "order");
    if (fileName) {
      formData.append('fileName', fileName);
    }
    return this.httpClient.post(this.serviceUrls.uploadFileUrl, formData).toPromise();
  }
}



